package com.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.constant.ReCommentConst;
import com.hc.constant.SkuConst;
import com.hc.domain.Comment;
import com.hc.domain.OrderItem;
import com.hc.domain.Recomment;
import com.hc.mapper.RecommentMapper;
import com.hc.service.CommentService;
import com.hc.service.OrderItemService;
import com.hc.service.OrderService;
import com.hc.service.RecommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author hc
 * @date 2021/12/24 16:15
 */
@Service
public class RecommentServiceImpl extends ServiceImpl<RecommentMapper, Recomment> implements RecommentService {

    @Resource
    private CommentService commentService;
    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;

    @Override
    @Transactional
    public void saveRecomment(Recomment recomment, String orderId, Integer skuId, Long userId) {
        //找到指定用户在指定订单中的指定商品的第一次评论
        Comment comment = commentService.getOne(new QueryWrapper<Comment>()
                .eq("user_id", userId)
                .eq("sku_id", skuId)
                .eq("order_id", orderId));

        recomment.setCommentId(comment.getId());
        recomment.setStatus(ReCommentConst.Status.SUBMIT_NOT_ALLOWED);
        //保存追加评论
        baseMapper.insert(recomment);

        OrderItem orderItem = OrderItem.builder()
                .comments(SkuConst.Comment.SECOND)
                .build();
        //修改OrderItem中的评论数量
        orderItemService.update(orderItem, new QueryWrapper<OrderItem>()
                .eq("order_id", orderId)
                .eq("sku_id", skuId));
    }
}
